<script>
  import requestWithJsonp from '@/utils/jsonp';
	export default {
		onLaunch: function() {
      /*#ifdef MP*/
      if(!uni.getStorageSync('token')){
        uni.handleLogin()
      }
      const updateManager = uni.getUpdateManager();
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        console.log(res.hasUpdate);
      });
      updateManager.onUpdateReady(function (res) {
        uni.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success(res) {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate();
            }
          }
        });
      
      });
      /*#endif*/
      uni.getLocation({
        type: 'wgs84',
        isHighAccuracy:true,
        success: (res) => {
          // #ifdef H5
          requestWithJsonp(`https://apis.map.qq.com/ws/geocoder/v1/?location=${res.latitude},${res.longitude}&key=MIUBZ-O3QR3-V5M3V-RVE7B-4A3VJ-67BFY&output=jsonp`)
          .then((allData) => {
            const {ad_info} = allData.result
            const data = {
              latitude:res.latitude,
              longitude:res.longitude,
              ...ad_info,
              name:ad_info.city,
              cityCode:ad_info.city_code.substring(ad_info.nation_code.length),
              timeStamp:Date.now()
            }
            if(!uni.getStorageSync('position').city){
              this.handleUpdateLoca(data)
              return;
            }else{
              uni.setStorageSync('position',{...uni.getStorageSync('position'),...data})
            }
            uni.setStorageSync('position',{...uni.getStorageSync('position'),...data})
            if(ad_info.city !== uni.getStorageSync('position').city){
              uni.showModal({
                title:`牙荷定位到您在${ad_info.city},是否切换?`,
                confirmText:`切换到${ad_info.city}`,
                confirmColor:'#20cebe',
                success: (ret) => {
                  if (ret.confirm) {
                    this.handleUpdateLoca(data)
                  }
                }
              })
            }
          }).catch(function (error) {
            throw new Error(`获取地址失败${error}`);
          });
          // #endif
          /*#ifdef MP*/
          uni.request({
            url:`https://apis.map.qq.com/ws/geocoder/v1/?location=${res.latitude},${res.longitude}&key=MIUBZ-O3QR3-V5M3V-RVE7B-4A3VJ-67BFY`,
            mode: 'no-cors',
            cache: 'no-cache',
            success: (allData) => {
              const {ad_info} = allData.data.result
              const data = {
                latitude:res.latitude,
                longitude:res.longitude,
                ...ad_info,
                name:ad_info.city,
                cityCode:ad_info.city_code.substring(ad_info.nation_code.length),
                timeStamp:Date.now()
              }
              if(!uni.getStorageSync('position').city){
                this.handleUpdateLoca(data)
                return;
              }else{
                uni.setStorageSync('position',{...uni.getStorageSync('position'),...data})
              }
              uni.setStorageSync('position',{...uni.getStorageSync('position'),...data})
              if(ad_info.city !== uni.getStorageSync('position').city){
                uni.showModal({
                  title:`牙荷定位到您在${ad_info.city},是否切换?`,
                  confirmText:'切换',
                  confirmColor:'#20cebe',
                  success: (ret) => {
                    if (ret.confirm) {
                      this.handleUpdateLoca(data)
                    }
                  }
                })
              }
            }
          })
          /*#endif*/
          if(!uni.getStorageSync('selectStore')){
            uni.reLaunch({
              url:'/mainPackage/change-position'
            })
          }
        },
        fail: (err) => {
          const data = {
            name:'武汉市',
            city:'武汉市',
            cityCode:'420100',
            timeStamp:Date.now()
          }
          this.handleUpdateLoca(data)
          if(!uni.getStorageSync('selectStore')){
            uni.reLaunch({
              url:'/mainPackage/change-position'
            })
          }
        }
      })

      // #ifdef H5
      // if(process.env.NODE_ENV == 'production') uni.warrantLogin()
      function onBridgeReady(){
        WeixinJSBridge.invoke('setFontSizeCallback', {'fontSize':0});
        WeixinJSBridge.on('menu:setfont', function(){
          WeixinJSBridge.invoke('setFontSizeCallback',{'fontSize':0})
        });
      }
      if (typeof WeixinJSBridge == "undefined"){
        if( document.addEventListener ){
          document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        }else if (document.attachEvent){
          document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
          document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
      }else{
        onBridgeReady()
      }
      // #endif
		},
		onShow: function() {
		},
		onHide: function() {

		},
    methods:{
      async handleUpdateLoca(data){
        const result = await this.$get('/merchant/v1/cities/selectCityName',{cityCode:data.cityCode})
        uni.setStorageSync('position',{...data,...result.content})
        uni.setStorageSync('cityInfo',{...data,...result.content})
        uni.$emit('updateLocation')
      }
    }
	}
</script>

<style lang="scss">
/*每个页面公共css */
@import '@/uni_modules/uni-scss/index.scss';

:root,page{
  --page-bg:#f5f5f5;
  --theme-color:#20cebe;
  --theme-color-10:#20cebe16;
  --sub-theme:#FF6737;
  --sub-theme-10:#FF673716;
  --border-color:#D1D1D1;
  --title-color:#3A3A3A;
  --sub-title:#B0B0B0;
  height: 100%;
}
::v-deep [v-cloak] {
  display: none!important;
}
.sub-theme{
  color: var(--sub-theme)!important;
}
.theme-color{
  color: var(--theme-color)!important;
}
uni-image{
  width: 100%;
  height: auto;
}
.box-flex{
  display: flex;
}
button{
  margin: 0;
  border-radius: 0;
  background: transparent;
}
button::after{
  display: none;
}
.uni-load-more__text{
  font-size: 24rpx!important;
  color: #C0C8CF!important;
}

.not-bar::-webkit-scrollbar {
  width: 0;
  height: 0;
  display: none;
  // -webkit-overflow-scrolling: touch;
}
.not-bar::-webkit-scrollbar-thumb {
  border-radius: 10px;
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
  background: #c4c4c4;
}

.dot{display: -webkit-box;display: box;overflow: hidden;text-overflow: ellipsis;word-break: break-all;-webkit-box-orient: vertical;-webkit-line-clamp: 1;line-clamp: 1;}
.dot2 {display: -webkit-box;display: box;overflow: hidden;text-overflow: ellipsis;word-break: break-all;-webkit-box-orient: vertical;-webkit-line-clamp: 2;line-clamp: 2;}
.dot3 {display: -webkit-box;display: box;overflow: hidden;text-overflow: ellipsis;word-break: break-all;-webkit-box-orient: vertical;-webkit-line-clamp: 3;line-clamp: 3;}
.dot4 {display: -webkit-box;display: box;overflow: hidden;text-overflow: ellipsis;word-break: break-all;-webkit-box-orient: vertical;-webkit-line-clamp: 4;line-clamp: 4;}


.var-button{
  line-height: 80rpx;
  background: linear-gradient( 90deg, #29D8C6 0%, #10BEAF 100%);
  color: #fff;
  text-align: center;
  font-size: 28rpx;
  border-radius: 80rpx;
}
.var-empty{
  padding: 60rpx;
  text-align: center;
  font-size: 26rpx;
  color: #6D6D6D;
  uni-image{
    width: 70%;
  }
  .h2{
    font-size: 36rpx;
    color: var(--title-color);
    font-weight: bold;
  }
  .img{
    width:132rpx;
    height: 132rpx;
  }
  button{
    background: #d6341e;
    color: #F8F8F8;
    border-radius: 60rpx;
    margin-top: 60rpx;
    line-height: 80rpx;
  }
  .p{
    color: #6D6D6D;
    font-size: 26rpx;
    margin-top: 20rpx;
  }
  .button{
    line-height: 1;
    font-size: 24rpx;
    border-radius: 100rpx;
    padding: 16rpx 20rpx;
    color: var(--theme-color);
    border: 2rpx solid var(--theme-color);
    margin: 78rpx auto;
    display: inline-block;
  }
}

.uni-nav-bar-text{
  font-size: 34rpx!important;
}
</style>
